const app = getApp()
var getPos = require('./getPosition.js')
function click_country (page) {
  var item = page.data.item
  var str1 = 'item.city_isClicked';
  var str2 = 'item.country_isClicked';
  var str3 = 'item.defaultVal'
  page.setData({
    [str1]: false,
    [str2]: true,
    [str3]: '全国'
  })
}
function click_city (page) {
  var item = page.data.item;
  if (app.globalData.articleCity != '') {
    var city = app.globalData.articleCity
    item.currentCity = city;
    item.defaultVal = city;
    var str1 = 'item.city_isClicked';
    var str2 = 'item.country_isClicked';
    page.setData({
      [str1]: true,
      [str2]: false,
      item: item
    })
  }
  if (item.currentCity === '本地') {
    wx.getSetting({
      success: (res) => {
        console.log(res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation'] == true || res.authSetting['scope.userLocation'] == undefined) {
          getPos.loadInfo(page)
        }
        if (page.data.item.userLocation == false) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '授权失败',
                  icon: 'success',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用getLocationt的API
                      getPos.loadInfo(page)
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  }
  else {
    var city = item.currentCity
    item.defaultVal = city
    var str1 = 'item.city_isClicked'
    var str2 = 'item.country_isClicked'
    page.setData({
      [str1]: true,
      [str2]: false,
      item: item
    })
  }
}
module.exports = {
  click_country: click_country,
  click_city: click_city
}